﻿using Quartz;
using QuartzDemo.ICore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QuartzDemo
{
    public class BadmatiJobListener : IBadmatiJobListener
    {

        public string Name
        {
            get { return "BadmatiJobListner"; }
        }

        public Action<JobExecuteMessage> NotifyExecute;
        public BadmatiJobListener(Action<JobExecuteMessage> action)
        {
            this.NotifyExecute = action;
        }
        /**
         * (1)
         * 任务执行之前执行
         * Called by the Scheduler when a JobDetail is about to be executed (an associated Trigger has occurred).
         */
        public void JobToBeExecuted(IJobExecutionContext context)
        {
            Console.WriteLine("MyJobListener.jobToBeExecuted()");
        }

        /**
         * (2)
         * 这个方法正常情况下不执行,但是如果当TriggerListener中的vetoJobExecution方法返回true时,那么执行这个方法.
         * 需要注意的是 如果方法(2)执行 那么(1),(3)这个俩个方法不会执行,因为任务被终止了嘛.
         * Called by the Scheduler when a JobDetail was about to be executed (an associated Trigger has occurred),
         * but a TriggerListener vetoed it's execution.
         */
        public void JobExecutionVetoed(IJobExecutionContext context)
        {
            Console.WriteLine("MyJobListener.jobExecutionVetoed()");
        }

        /**
         * (3)
         * 任务执行完成后执行,jobException如果它不为空则说明任务在执行过程中出现了异常
         * Called by the Scheduler after a JobDetail has been executed, and be for the associated Trigger's triggered(xx) method has been called.
         */
        public void JobWasExecuted(IJobExecutionContext context,
                JobExecutionException jobException)
        {
            var msg = new JobExecuteMessage();
            msg.JobName = this.Name;
            msg.IsSuccess = true;
            msg.ExecuteTime = DateTime.Now;
            msg.ExecuteException = jobException;
            NotifyExecute(msg);
        }


    }
}
